/**
 * 作者：陈文斌 on 2019/1/9.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'list_tile_normal.dart';

class ListTileTextField extends StatefulWidget {
  /// 标题
  final Widget title;

  /// 输入框默认内容
  final String content;

  /// 输入提示
  final String hintText;

  /// 焦点
  final FocusNode focusNode;

  /// 键盘确认键样式
  final TextInputAction textInputAction;

  /// 点击输入框
  final GestureTapCallback onTap;

  /// 输入框文字改变时的回调
  final ValueChanged<String> onChanged;

  /// 完成输入时的回调
  final VoidCallback onEditingComplete;

  /// 键盘弹起的回调
  final Function hasFocusCallback;

  /// 键盘收起的回调
  final Function unFocusCallback;

  /// 是否显示必填提示
  final bool requiredVisible;

  /// 是否隐藏下划线
  final bool dividerVisible;

  ListTileTextField({
    Key key,
    @required this.title,
    @required this.content,
    this.hintText = '请输入',
    this.focusNode,
    this.onTap,
    this.onChanged,
    this.onEditingComplete,
    this.hasFocusCallback,
    this.unFocusCallback,
    this.textInputAction = TextInputAction.done,
    this.requiredVisible = true,
    this.dividerVisible = false,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => _ListTileTextFieldState();
}

class _ListTileTextFieldState extends State<ListTileTextField> {
  final TextStyle _trailingStyle = TextStyle(
    color: AppColors.PlaceholderTextColor,
    fontSize: AppFontSizes.Font15,
    fontFamily: AppFontFamilies.PingFangSC,
    fontWeight: FontWeight.w500,
  );

  /// 输入框焦点
  FocusNode _focusNode;

  FocusNode get _effectiveFocusNode =>
      widget.focusNode ?? (_focusNode ??= FocusNode());

  /// 键盘弹出收起监听
  Future<Null> _focusNodeListener() async {
    if (_effectiveFocusNode.hasFocus) {
      if (widget.hasFocusCallback != null) {
        widget.hasFocusCallback();
      }
    } else {
      if (widget.unFocusCallback != null) {
        widget.unFocusCallback(widget.title);
      }
    }
  }

  Widget _requiredPrompt() {
    return Offstage(
      offstage: widget.requiredVisible,
      child: Container(
        height: 19,
        padding: EdgeInsets.only(left: 6, top: 4),
        child: Text(
          '*',
          style: TextStyle(
            color: Color(0xFFFF1212),
            fontSize: AppFontSizes.Font15,
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    _effectiveFocusNode.addListener(_focusNodeListener);
    super.initState();
  }

  @override
  void dispose() {
    _effectiveFocusNode.removeListener(_focusNodeListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final FocusNode focusNode = _effectiveFocusNode;
    final _content = widget.content ?? '';

    // 输入框管理器
    TextEditingController contentController =
    new TextEditingController.fromValue(
      TextEditingValue(
        text: _content,
        selection: new TextSelection.fromPosition(
          TextPosition(
              affinity: TextAffinity.downstream, offset: _content.length),
        ),
      ),
    );

    return ListTileNormal(
      key: widget.key,
      title: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          widget.title,
          _requiredPrompt(),
        ],
      ),
      trailing: Container(
        child: TextField(
          controller: contentController,
          focusNode: focusNode,
          onChanged: widget.onChanged,
          onEditingComplete: widget.onEditingComplete,
          textInputAction: widget.textInputAction,
          decoration: InputDecoration(
            contentPadding: EdgeInsets.all(0),
            hintText: widget.hintText,
            border: InputBorder.none,
            hintStyle: _trailingStyle,
          ),
          style: _trailingStyle,
          textAlign: TextAlign.right,
        ),
      ),
      arrowVisible: true,
      dividerVisible: widget.dividerVisible,
    );
  }
}
